JDOM读写XML文件实例 - 岳巍的专栏 - 博客频道 - CSDN.NET

创建时间:2015/8/28 22:12
来源:http://blog.csdn.net/java_cxrs/article/details/5474815


转自:http://www.javaeye.com/topic/170511

需要的jar包 jdom.jar

下载地址:http://jdom.org

1.读取xml文件中的元素

a. abc.xml

 

  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <messages>  
  3. <message id="1">  
  4.    <title>11</title>  
  5.    <content>  
  6.     <name>lvpingyu</name>  
  7.     <age>23</age>  
  8.    </content>  
  9.    <email>33</email>  
  10. </message>  
  11. <message id="2">  
  12.    <title>44</title>  
  13.    <content>  
  14.     <name>lvpingyu</name>  
  15.     <age>23</age>  
  16.    </content>  
  17.    <email>66</email>  
  18. </message>  
  19. </messages>  

 

b.如何读取 代码如下:

  1. package test;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.util.List;  
  5.   
  6. import org.jdom.Document;  
  7. import org.jdom.Element;  
  8. import org.jdom.input.SAXBuilder;  
  9.   
  10. public class MyJdom {  
  11.     public static void main(String[] args) throws Exception {  
  12.         SAXBuilder sb = new SAXBuilder();//建立构造器  
  13.         String path="abc.xml";  
  14.         Document doc = sb.build(new FileInputStream(path));//读入指定文件  
  15.         Element root = doc.getRootElement();//获得根节点  
  16.         List list = root.getChildren();//将根节点下的所有子节点放入List中  
  17.         for (int i = 0; i < list.size(); i++) {  
  18.             System.out.println("---------------------------");  
  19.             Element item = (Element) list.get(i);//取得节点实例  
  20.             String id = item.getAttribute("id").getValue();//取得属性值  
  21.             System.out.println("id-->" + id);  
  22.             Element sub = item.getChild("title");//取得当前节点的子节点  
  23.             String text = sub.getText();//取得当前节点的值  
  24.             System.out.println("Title-->" + text);  
  25.             if (item.getChild("content").getChildren().size() > 0) {  
  26.                 Element sub2 = item.getChild("content").getChild("name");  
  27.                 String text2 = sub2.getText();  
  28.                 System.out.println("name-->" + text2);  
  29.             }  
  30.             Element sub3 = item.getChild("email");  
  31.             String text3 = sub3.getText();  
  32.             System.out.println("Email-->" + text3);  
  33.           
  34.         }  
  35.                         System.out.println("---------------------------");  
  36.     }  
  37.   
  38. }  

 

2.生成xml文件

  1. package test;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import org.jdom.Document;  
  8. import org.jdom.Element;  
  9. import org.jdom.output.XMLOutputter;  
  10.   
  11. public class JavaXML {  
  12.     public void buildXML() throws Exception {  
  13.         String path = "company_list.xml";  
  14.         // 创建根节点 list;      
  15.         Element root = new Element("list");  
  16.         //根节点添加到文档中;      
  17.         Document Doc = new Document(root);  
  18.         //   此处 for 循环可替换成 遍历 数据库表的结果集操作;      
  19.         for (int i = 0; i < 5; i++) {  
  20.             // 创建子节点 company;  
  21.             Element elements = new Element("company");  
  22.             // 给 company 节点添加属性 id;  
  23.             elements.setAttribute("id""" + i);  
  24.             // 给 company 节点添加子节点并赋值  
  25.               
  26. // new Element("company_name")中的 "company_name"   
  27. //替换成表中相应字段,setText("name")中 "name 替换成表中记录值;   
  28.             elements.addContent(new Element("company_name").setText("name" + i));  
  29.             elements.addContent(new Element("company_email").setText("@" + i  
  30.                     + ".com"));  
  31.   
  32.             // 给父节点list添加company子节点;     
  33.             root.addContent(elements);  
  34.         }  
  35.         XMLOutputter XMLOut = new XMLOutputter();  
  36.         // 输出company_list.xml文件;     
  37.         XMLOut.output(Doc, new FileOutputStream(path));  
  38.     }  
  39.   
  40.     public static void main(String[] args) {  
  41.         JavaXML javaXML = new JavaXML();  
  42.         try {  
  43.             javaXML.buildXML();  
  44.         } catch (Exception e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.     }  
  50.   
  51. }  

 

可参考的资料:参考资料:
    JDOM V1.0 API在线参考手册(http://www.jdom.org/docs/apidocs/index.html)
    JDOM V1.0下载(http://www.jdom.org/dist/binary/jdom-1.0.zip)
    JDOM处理XML快速上手
(http://kingwong.blogdriver.com/kingwong/291878.html)

版权声明:本文为博主原创文章,未经博主允许不得转载。

.